/***************************************************************************
* Lyrics-Player                                                            *
* Copyright (C) 2012 Andrey Derevyanko <derandr@gmail.com>                 *
*                                                                          *
* This program is free software: you can redistribute it and/or modify     *
* it under the terms of the GNU General Public License as published by     *
* the Free Software Foundation, either version 3 of the License, or        *
* (at your option) any later version.                                      *
*                                                                          *
* This program is distributed in the hope that it will be useful,          *
* but WITHOUT ANY WARRANTY; without even the implied warranty of           *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
* GNU General Public License for more details.                             *
*                                                                          *
* You should have received a copy of the GNU General Public License        *
* along with this program.  If not, see <http://www.gnu.org/licenses/>.    *
***************************************************************************/

/*! \file id3Lib.cpp
 *  \brief Functions for metadata access using id3 library implementation
 *  \details Namespace contains reader and writer access classes with the corresponding functions
 *  \author Andrey Derevyanko <derandr@gmail.com>
 *  \version 1.0
 *  \date 15/10/2012
 *  \copyright GNU Public License
 */

#include "id3Lib.h"

namespace id3Lib{

id3Reader::id3Reader(){
}

id3Reader::~id3Reader(){
}

void id3Reader::newSourceFile(QString fileName){
    trackMetaData_t data;
    ID3_Tag *readTag;
    size_t genreNumber;

    readTag = new ID3_Tag;

    readTag->Clear();
    readTag->Link(fileName.toLatin1());
    data.m_filePath = fileName;
    data.m_title = ID3_GetTitle(readTag);
    data.m_artist = ID3_GetArtist(readTag);
    data.m_album = ID3_GetAlbum(readTag);
    data.m_year = ID3_GetYear(readTag);
    genreNumber = ID3_GetGenreNum(readTag);
    if (genreNumber == 255)
        data.m_genre = ID3_GetGenre(readTag);
    else
        data.m_genre = id3TagConverter::numToGenre(genreNumber);
    emit newMetaData(data);

    delete readTag;


    /*
    if ((readTag = ID3Tag_New()) != NULL)
    {
        ID3Tag_Clear(readTag);
        (void) ID3Tag_Link(readTag, fileName.toLatin1());

        data.m_title = readTagField(readTag, ID3FID_TITLE);
        data.m_artist = readTagField(readTag, ID3FID_LEADARTIST);
        data.m_album = readTagField(readTag, ID3FID_ALBUM);
        data.m_year = readTagField(readTag, ID3FID_YEAR);
        data.m_genre = readTagField(readTag, ID3FID_MOOD);

        emit newMetaData(data);
    }
    */
}

/*
QString id3Reader::readTagField(ID3Tag *tag, ID3_FrameID id){
    ID3Frame *frame;
    if ((frame = ID3Tag_FindFrameWithID(tag, id)) != NULL)
    {
        ID3Field *field;
        if ((field = ID3Frame_GetField(frame, ID3FN_TEXT)) != NULL)
        {
            char title[1024];
            if (id == ID3FID_MOOD){
                return QString::number(ID3Field_GetINT(field));
            }
            else {
                (void) ID3Field_GetASCII(field, title, 1024);
                return title;
            }
        }
    }
    return QString();
}
*/

id3Writer::id3Writer(){

}

id3Writer::~id3Writer(){

}

void id3Writer::setMetaData(QString fileName, trackMetaData_t data){
    ID3_Tag *writeTag;
    writeTag = new ID3_Tag;

    writeTag->Clear();
    writeTag->Link(fileName.toLatin1());

    ID3_AddArtist(writeTag, data.m_artist.toLatin1(), true);
    ID3_AddTitle(writeTag, data.m_title.toLatin1(), true);
    ID3_AddAlbum(writeTag, data.m_album.toLatin1(), true);
    ID3_AddYear(writeTag, data.m_year.toLatin1(), true);
    ID3_AddGenre(writeTag, id3TagConverter::genreToNum(data.m_genre.toLatin1()), true);

    writeTag->Update();

    delete writeTag;
}

const QString id3TagConverter::genreDescription[number_of_genres] = {
  "Blues",             //0
  "Classic Rock",      //1
  "Country",           //2
  "Dance",             //3
  "Disco",             //4
  "Funk",              //5
  "Grunge",            //6
  "Hip-Hop",           //7
  "Jazz",              //8
  "Metal",             //9
  "New Age",           //10
  "Oldies",            //11
  "Other",             //12
  "Pop",               //13
  "R&B",               //14
  "Rap",               //15
  "Reggae",            //16
  "Rock",              //17
  "Techno",            //18
  "Industrial",        //19
  "Alternative",       //20
  "Ska",               //21
  "Death Metal",       //22
  "Pranks",            //23
  "Soundtrack",        //24
  "Euro-Techno",       //25
  "Ambient",           //26
  "Trip-Hop",          //27
  "Vocal",             //28
  "Jazz+Funk",         //29
  "Fusion",            //30
  "Trance",            //31
  "Classical",         //32
  "Instrumental",      //33
  "Acid",              //34
  "House",             //35
  "Game",              //36
  "Sound Clip",        //37
  "Gospel",            //38
  "Noise",             //39
  "AlternRock",        //40
  "Bass",              //41
  "Soul",              //42
  "Punk",              //43
  "Space",             //44
  "Meditative",        //45
  "Instrumental Pop",  //46
  "Instrumental Rock", //47
  "Ethnic",            //48
  "Gothic",            //49
  "Darkwave",          //50
  "Techno-Industrial", //51
  "Electronic",        //52
  "Pop-Folk",          //53
  "Eurodance",         //54
  "Dream",             //55
  "Southern Rock",     //56
  "Comedy",            //57
  "Cult",              //58
  "Gangsta",           //59
  "Top 40",            //60
  "Christian Rap",     //61
  "Pop/Funk",          //62
  "Jungle",            //63
  "Native American",   //64
  "Cabaret",           //65
  "New Wave",          //66
  "Psychadelic",       //67
  "Rave",              //68
  "Showtunes",         //69
  "Trailer",           //70
  "Lo-Fi",             //71
  "Tribal",            //72
  "Acid Punk",         //73
  "Acid Jazz",         //74
  "Polka",             //75
  "Retro",             //76
  "Musical",           //77
  "Rock & Roll",       //78
  "Hard Rock",         //79
// following are winamp extentions
  "Folk",                  //80
  "Folk-Rock",             //81
  "National Folk",         //82
  "Swing",                 //83
  "Fast Fusion",           //84
  "Bebob",                 //85
  "Latin",                 //86
  "Revival",               //87
  "Celtic",                //88
  "Bluegrass",             //89
  "Avantgarde",            //90
  "Gothic Rock",           //91
  "Progressive Rock",      //92
  "Psychedelic Rock",      //93
  "Symphonic Rock",        //94
  "Slow Rock",             //95
  "Big Band",              //96
  "Chorus",                //97
  "Easy Listening",        //98
  "Acoustic",              //99
  "Humour",                //100
  "Speech",                //101
  "Chanson",               //102
  "Opera",                 //103
  "Chamber Music",         //104
  "Sonata",                //105
  "Symphony",              //106
  "Booty Bass",            //107
  "Primus",                //108
  "Porn Groove",           //109
  "Satire",                //110
  "Slow Jam",              //111
  "Club",                  //112
  "Tango",                 //113
  "Samba",                 //114
  "Folklore",              //115
  "Ballad",                //116
  "Power Ballad",          //117
  "Rhythmic Soul",         //118
  "Freestyle",             //119
  "Duet",                  //120
  "Punk Rock",             //121
  "Drum Solo",             //122
  "A capella",             //123
  "Euro-House",            //124
  "Dance Hall",            //125
  "Goa",                   //126
  "Drum & Bass",           //127
  "Club-House",            //128
  "Hardcore",              //129
  "Terror",                //130
  "Indie",                 //131
  "Britpop",               //132
  "Negerpunk",             //133
  "Polsk Punk",            //134
  "Beat",                  //135
  "Christian Gangsta Rap", //136
  "Heavy Metal",           //137
  "Black Metal",           //138
  "Crossover",             //139
  "Contemporary Christian",//140
  "Christian Rock ",       //141
  "Merengue",              //142
  "Salsa",                 //143
  "Trash Metal",           //144
  "Anime",                 //145
  "JPop",                  //146
  "Synthpop"               //147
};

}
